package com.example.shootinggamebackend.service.impl;

import com.example.shootinggamebackend.service.SpeechRecognizeService;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.example.shootinggamebackend.service.TextLLMService;
import io.reactivex.Flowable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SpeechRecognizeServiceImpl implements SpeechRecognizeService {

    @Override
    public String callMultiModalConversation(String recordUrl) {
        // 仅当模拟时启用此逻辑
        recordUrl = "/code/videos/" + recordUrl +".mp3";

        try {
            String prompt = "这份录音在说什么？";

            MultiModalConversation conv = new MultiModalConversation();
            String finalRecordUrl = recordUrl;
            MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                    .content(Arrays.asList(new HashMap<String, Object>() {{
                                               put("audio", finalRecordUrl);
                                           }},
                            Collections.singletonMap("text", prompt))).build();
            MultiModalConversationParam param = MultiModalConversationParam.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .model("qwen2-audio-instruct")
                    .message(userMessage)
                    .build();
            Flowable<MultiModalConversationResult> result = conv.streamCall(param);
            String res = (String) result.blockingLast().getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text");
            return res;
        } catch (ApiException e) {
            System.err.println("调用语音识别大模型 Api 异常");
            e.printStackTrace();
        } catch (NoApiKeyException e) {
            System.err.println("调用语音识别大模型缺失 api key");
        } catch (UploadFileException e) {
            System.err.println("调用语音识别大模型上传文件异常");
        }
        return "识别失败";
    }

    @Override
    // 正则匹配数字
    public List<Integer> extractNumbers(String input) {
        String regex = "\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        List<Integer> numbers = new ArrayList<>();
        while (matcher.find()) {
            numbers.add(Integer.parseInt(matcher.group()));
        }
        return numbers;
    }

}
